#define __DEBUG__
// by Xeno
#define THIS_FILE "fn_makeuav.sqf"
#include "x_setup.sqf"

private ["_has_gps","_uav"];

if (!alive player || {__pGetVar(xr_pluncon)}) exitWith {};

_has_gps = false;

_exitj = false;
if (GVAR(with_ranked)) then {
	if (score player < (GVAR(ranked_a) select 19)) then {
		(format [(localize "STR_DOM_MISSIONSTRING_76b"), score player,GVAR(ranked_a) select 19]) call FUNC(HQChat);
		_exitj = true;
	} else {
		[QGVAR(pas), [player, (GVAR(ranked_a) select 19) * -1]] call FUNC(NetCallEventCTS);
	};
};
if (_exitj) exitWith {};

if !(GVAR(UAV_Terminal) in (assignedItems player)) then {
	_has_gps = "ItemGPS" in (assignedItems player);
	__pSetVar [QGVAR(has_gps), true];
	player linkItem GVAR(UAV_Terminal);
};

// TYRGHEN
//_uav = [position player, 0, GVAR(UAV_Small), side (group player)] call BIS_fnc_spawnVehicle;
_position = position player;
_position set [2,5];
_uav = createVehicle [GVAR(UAV_Small), _position, [], 0, "FLY"];
__TRACE_1("","_uav");
createVehicleCrew _uav;

// TYRGHEN
if (!(player connectTerminalToUav _uav)) exitWith {
	{ deleteVehicle _x;
	} foreach (crew _uav);
	deleteVehicle _uav;
};
_uav = [_uav, crew _uav, group _uav];
__TRACE_1("","_uav");

player action ["UAVTerminalOpen"];

// TYRGHEN - gives errors in markercheck otherwise
[QGVAR(p_o_a2), [GVAR(string_player), (_uav select 0)]] call FUNC(NetCallEventCTS);

(_uav select 0) spawn {
	private "_uav";
	PARAMS_1(_uav);
	__TRACE_1("_uav","_uav");
	while {!isNull (getConnectedUav player) && {alive player} && {!__pGetVar(xr_pluncon)} && {alive _uav} && {canMove _uav}} do {
		sleep 1.1;
	};
	
	if (!isNull _uav) then {
		// TYRGHEN - gives errors in markercheck otherwise
		[QGVAR(p_o_a2r), [GVAR(string_player),_uav]] call FUNC(NetCallEventCTS);
	};
	{
		deleteVehicle _x;
	} forEach [_uav] + (_this select 1);
	(findDisplay 160) closeDisplay 1;
};
